package ShopStyle.htc;

import android.content.Context;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MagView extends SurfaceView implements SurfaceHolder.Callback {

	//For maintain the surface 
	private int width , height;
	private float scale = 1.0f;
	private Paint textPaint = new Paint();
	private static final String START_TEXT = "MagView Start";
	private static final String PACKAGE_NAME = "ShopStyle.htc" ;
	
	private Context _context;
	private Drawable pic1;
	
	public String backgroundName;
	public MagItem[] items;
	private Drawable _picBackground;
	private int _heightBackground;
	private int _widthBackground;
	
	
	
	public MagView(Context context) {
		super(context);
		_context= context;
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);

		setFocusable(true); // make sure we get key events
		setFocusableInTouchMode(true); // make sure we get touch events
		
		//Initialize MagItem
		items = new MagItem[6];	//Maximum for 6 product.
		for (int i=0;i<items.length ;++i)
			items[i] = null;
		
	}
	
	
	public void setBackground(String backgroundFilename) {
		
		int resID = _context.getResources().getIdentifier( backgroundFilename , "drawable", _context.getPackageName() );
		_picBackground = _context.getResources().getDrawable( resID );
		if (_picBackground!=null) {
			//Log.e("MagItem" , "Allocated resource [" + backgroundFilename + "]" + _picBackground.toString());
			_heightBackground = _picBackground.getIntrinsicHeight();
			_widthBackground = _picBackground.getIntrinsicWidth();
			//Log.e("MagItem" , "  H[" + _heightBackground + "] W[" + _widthBackground + "]" );
			_picBackground.setBounds(0, 0, _widthBackground , _heightBackground);
			_picBackground.setAlpha(240);
		}
		else {
			Log.e("MagItem" , "Allocated resource [" + backgroundFilename + "] Failed" );
			
		}
	}
	public boolean addMagItem( MagItem m ) {
		int i;
		for (i=0;i<items.length ;++i) {
			if (items[i]==null) //Avaliable
				break;
		}
		if (i==items.length ) return false;	//Fail to add new MagItem
		
		//Log.e ("@addMagItem", "index :" + i );
		items[i] = m;
		return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,	int height) {
		// TODO Auto-generated method stub
		this.width = width;
		this.height = height;
		if (width > height) {
			this.scale = width / 480f;
		} else {
			this.scale = height / 480f;
		}

		Canvas c = getHolder().lockCanvas();
		if (c == null) return ;

		//Display background
		_picBackground.draw(c);
		//Display items
		//MagItem mtest = new MagItem( _context , "ship", "Line1" , "Line2" , "Line3");
		//mtest.drawOnCanvas(c, 300, 300, 255);
		//mtest.drawOnCanvas(c, 370, 340, 128);
		for (int i=0;i<items.length ;++i ) {
			if (items[i]==null) break;
			//Log.e("@drawItem" , i + "  x:" +items[i]._x + " , y:" + items[i]._y );
			items[i].drawOnCanvas(c);
			//mtest.drawOnCanvas(c, 300, 300, 255);
			//getHolder().unlockCanvasAndPost(c);
		}
		getHolder().unlockCanvasAndPost(c);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int pointerCount = event.getPointerCount();
		//I assume only "one" click on the screen...
		//Log.e("@onTouchEvent" , "..start.");
		if (pointerCount>1) return true;
		
		//Log.e("@onTouchEvent" , "...");
		//
		for (int i=0;i<items.length;++i) {
			//Check if we were in this item ?
			if (items[i]==null) break;
			int localX = (int) event.getX(0) - items[i].getX();
			int localY = (int) event.getY(0) - items[i].getY();
			items[i].checkEvent(localX, localY , "item" + i );
		}

		return true;
	}
	
	

}
